﻿using ArtMath.Core.Helper;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using ArtMath.CommonType;

namespace ArtMath.Control
{
    /// <summary>
    /// 表示元件夹的层叠列表项
    /// </summary>
    [Serializable]
    public class FolderLayerItem : LayerItem
    {
        [Browsable(false)]
        public List<LayerItem> Children { get; } = new List<LayerItem>();
        public FolderLayerItem(string name) : base(name, null)
        {
            Name = Utility.GetNextAvailableName(name, CheckFolderNameExist);
            HostChanged += () =>
            {
                foreach (LayerItem item in Children)
                {
                    if (Host == null)
                    {
                        item.IndentLevel--;
                    }
                    else
                    {
                        item.IndentLevel++;
                    }
                }
            };
        }
        public bool CheckFolderNameExist(string name)
        {
            foreach (LayerItem item in Layer.Items)
            {
                if (item.ObjectState == ObjectState.Normal && item.Name == name && item is FolderLayerItem)
                {
                    return true;
                }
            }
            return false;
        }
        public override bool LinkPad
        {
            get
            {
                return false;
            }
        }
        public override void View_MouseDown(object sender, MouseEventArgs e)
        {
            int visibleCount = 0, inVisibleCount = 0;
            foreach (LayerItem item in Children)
            {
                if (item.LinkedObjectVisible)
                {
                    visibleCount++;
                }
                else
                {
                    inVisibleCount++;
                }
            }
            if (visibleCount == 0 || inVisibleCount == 0)
            {
                base.View_MouseDown(sender, e);
                foreach (LayerItem item in Children)
                {
                    item.View_MouseDown(sender, e);
                }
            }
            else
            {
                if (visibleCount >= inVisibleCount)
                {
                    if (LinkedObjectVisible)
                    {
                        base.View_MouseDown(sender, e);
                    }
                    foreach (LayerItem item in Children)
                    {
                        if (item.LinkedObjectVisible)
                        {
                            item.View_MouseDown(sender, e);
                        }
                    }
                }
                else
                {
                    if (!LinkedObjectVisible)
                    {
                        base.View_MouseDown(sender, e);
                    }
                    foreach (LayerItem item in Children)
                    {
                        if (!item.LinkedObjectVisible)
                        {
                            item.View_MouseDown(sender, e);
                        }
                    }
                }
            }
        }
    }
}
